<?php

class SbannerController extends AdminCController
{
	public $defaultAction ='list';
	
	public function actionCreate()
	{
		
		$model = new Banner('create');
		
		if(isset($_POST['Banner'])){
			$model -> attributes = $_POST['Banner'];
			
			$upload = UploadedFile::getInstance ( $model, 'Banner_Img' );
			$model -> Banner_Img = $upload;
			
			if($model -> validate()){
				
				$ext = $upload->getExtensionName ();
				$name = md5 ( $upload->getName () . time () );
				$name = $name . '.' . $ext;
				$path = Yii::app ()->getParams ();
				$UploadPath = $path->static_img;
				$folder = UploadedFile::getFolder ( $UploadPath );
				$upload->saveAs ( $folder . '/' . $name );
				
				$img_url = $folder . '/' . $name;
				
				$model -> Banner_Img = str_replace($UploadPath, '', $img_url);
				
				if($model -> save()){
					
					Yii::app() -> user -> setFlash('success','保存成功!');
					$this -> redirect($this -> createUrl('/sadmin/sbanner/create'));
					
				}
			}
		}
		
		$this->render('create',array('banner_model'=>$model));
	}

	public function actionDelete($id)
	{
		if($id){
			$this->loadModel($id)->delete();
			
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}else{
			$this -> redirect($this -> createUrl('/sadmin/sbanner'));
		}
	}

	public function actionList()
	{
		$model = new Banner();
		$dataProvider = $model ->search(); 
		$this->render('list',array('dataProvider'=>$dataProvider));
	}

	public function actionUpdate($id)
	{
		
		$model = Banner::model() -> findByPk($id);
		
		if(isset($_POST['Banner'])){
			
			$old_img = $model -> Banner_Img;
			
			$model -> attributes = $_POST['Banner'];
				
			$upload = UploadedFile::getInstance ( $model, 'Banner_Img' );
			if($upload){
				$model -> Banner_Img = $upload;
			}else{
				$model -> Banner_Img = $old_img;
			}
				
			if($model -> validate()){
		
				if($upload){
					$ext = $upload->getExtensionName ();
					$name = md5 ( $upload->getName () . time () );
					$name = $name . '.' . $ext;
					$path = Yii::app ()->getParams ();
					$UploadPath = $path->static_img;
					$folder = UploadedFile::getFolder ( $UploadPath );
					$upload->saveAs ( $folder . '/' . $name );
			
					$img_url = $folder . '/' . $name;
			
					$model -> Banner_Img = str_replace($UploadPath, '', $img_url);
				}
		
				if($model -> update()){
						
					Yii::app() -> user -> setFlash('success','修改成功!');
					$this -> redirect($this -> createUrl('/sadmin/sbanner/list'));
						
				}
			}
		}
		
		$this->render('update',array('banner_model'=>$model));
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Banner::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}